import {CALL_API, COMMON} from "../constants";
import {Token} from "../utils/Storage";
import {postMethod} from "../utils/fetch";

//获取微信信息
const fetchId = code => ({
    [CALL_API]: {
        url: `wechat/getWeChatUserInfo`,
        types: COMMON.id.types(),
        options: postMethod({code}),
        errorStatus: () => true,
        extra: r => Object.assign(r, {
            data: {
                name: r.data.nickname,
                appId: r.data.openId,
                avatar: r.data.headimgurl,
            }
        })
    }
});

//获取会员信息
const fetchVip = () => ({
    [CALL_API]: {
        url: `VMembers/selectMIdByWxId?wx_id=${Token.getAppId()}`,
        types: COMMON.vip.types(),
        errorStatus: () => true,
        extra: r => Object.assign(r, {
            data: {
                isRegistered: r.code === '401' ? false : true,
                userId: _.get(r.data, 'm_id', '') + '',
                isAdmin: _.get(r.data, 'boss', '') === "1",
            }
        })
    }
});

export const loadVip = () => dispatch => dispatch(fetchVip());
export const loadAppId = code => dispatch => dispatch(fetchId(code));
